<?php

namespace App\Console\Commands;

use App\Imports\MultipleImport;
use App\Models\ModelNumber;
use App\Models\ModelNumberSpecification;
use App\Models\Product;
use App\Models\ProductModelNumber;
use App\Models\ProductPrice;
use App\Models\Specification;
use Illuminate\Console\Command;
use Maatwebsite\Excel\Facades\Excel;

class AddProductUnitPrice extends Command
{
    /**
     * 导入添加产品单价
     * php artisan command:addproductunitprice
     *
     * @var string
     */
    protected $signature = 'command:addproductunitprice';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $file = public_path('excel').'/产品单价.xlsx';//获取文件路径
        //$file = public_path('excel').'/产品单价-软床.xlsx';//获取文件路径
        //$file = public_path('excel').'/产品单价-床垫.xlsx';//获取文件路径
        $import = new MultipleImport(3);
        Excel::import($import, $file);
        $results = $import->sheet[0]->data->toArray();
        //$results2 = $import->sheet[1]->data->toArray();
        //$results3 = $import->sheet[2]->data->toArray();
        //dd($results,$results2,$results3);
        unset($results[0]);
        unset($results[1]);
        $spe_arr = [
            '1.8*2米','1.8*2.2米','2*2.2米'
        ];
        foreach($results as $k=>$v){
            $this->info('导入：产品名称='.$v[1].";型号=".$v[2]."；单、价=".$v[5]);
            //$this->info('导入：产品名称='.$v[1].";型号=".$v[2]."；1.8*2米=".$v[5].";2*2.2米=".$v[6].";1.8*2.2米=".$v[7]);
            $product_obj = Product::updateOrCreate(['product_name'=>$v[1]],['product_name'=>$v[1]]);
            $model_number_obj = ModelNumber::updateOrCreate(['model_number'=>$v[2]],['model_number'=>$v[2]]);
            foreach($spe_arr as $kk=>$vv){
                $specification_obj = Specification::where('specification',$vv)->first();
                //p($specification_obj);
                if($vv == '1.8*2米'){
                    $unit_price = $v[5];
                }elseif ($vv == '1.8*2.2米'){
                    $unit_price = $v[7];
                }elseif ($vv == '2*2.2米'){
                    $unit_price = (int)$v[5]+50;
                    //$unit_price = $v[6];
                }
                $this->info('导入：产品名称='.$v[1].";型号=".$v[2].";规格：".$vv."；单价=".$unit_price);
                ProductPrice::updateOrCreate(
                    [
                        'model_number_id'=>$model_number_obj->id,
                        'specification_id'=>$specification_obj->id,
                        'product_id'=>$product_obj->id
                    ],
                    [
                        'model_number_id'=>$model_number_obj->id,
                        'specification_id'=>$specification_obj->id,
                        'product_id'=>$product_obj->id,
                        'unit_price'=>$unit_price
                    ]
                );
            }

            //沙发
//            $unit_price = $v[5];
//            $this->info('导入：产品名称='.$v[1].";型号=".$v[2]."；单价=".$unit_price);
//
//            ProductPrice::updateOrCreate(
//                [
//                    'model_number_id'=>$model_number_obj->id,
//                    'product_id'=>$product_obj->id
//                ],
//                [
//                    'model_number_id'=>$model_number_obj->id,
//                    'product_id'=>$product_obj->id,
//                    'unit_price'=>$unit_price
//                ]
//            );





        }
    }
}
